package logic

import (
	"context"
	"gorm.io/gorm"
	"rpc/models/mysql"

	"rpc/day18"
	"rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodsLogic) AddGoods(in *day18.AddGoodsRequest) (*day18.AddGoodsResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Goods{
		Model:      gorm.Model{},
		GoodsTitle: in.GoodsTitle,
		GoodsStock: int(in.GoodsStock),
		GoodsPrice: float64(in.GoodsPrice),
		GoodsCover: in.GoodsCover,
		GoodsState: int(in.GoodsState),
	}
	err := g.Create()
	if err != nil {
		logx.Error("商品添加失败")
		return nil, err
	}
	return &day18.AddGoodsResponse{
		GoodsId: int64(g.ID),
	}, nil
}
